﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class uc_ThamSo : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai bao bien
        TextMessageBox box = new TextMessageBox();
        ThamSo_BLL ts_bll = new ThamSo_BLL();
        #endregion

        #region LoadForm
        public uc_ThamSo()
        {
            InitializeComponent();
        }

        private void uc_ThamSo_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region cac chuc nang

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_ThamSo frm = new frm_ThamSo();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
                
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    frm_ThamSo frm = new frm_ThamSo();
                    frm.maThamSo = box.ToInt(grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaThamSo));
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadData();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                DataTable dt1;
                DataTable dt2;
                ThamSo_Public ts = new ThamSo_Public();
                try
                {
                    int ma = box.ToInt(grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle,colMaThamSo));
                    ts.MaThamSo = ma;
                    string ten = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colTenThamSo).ToString();
                    dt1 = ts_bll.TaoBang(box.sqlCheckExist("DM_Lop","MaThamSo",ma));
                    dt2 = ts_bll.TaoBang(box.sqlCheckExist("CTDG_GiaoVien", "MaThamSo", ma));
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count==0)
                        XtraMessageBox.Show(box.NotDelete(ma),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    if (dt1.Rows.Count == 0 && dt2.Rows.Count > 0)
                        XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    else if (dt1.Rows.Count == 0 && dt2.Rows.Count == 0)
                    {
                        if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            try
                            {
                                ts_bll.Delete(ts);
                                LoadData();
                            }
                            catch (Exception)
                            {
                                XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter, box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        #endregion

        #region Ham viet them
        private void LoadData()
        {
            grd_Data.DataSource = ts_bll.LayDuLieu();
        }
        #endregion
    }
}
